package cn.stylefeng.guns.modular.basic.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicLoadUnloadLabor;
import cn.stylefeng.guns.modular.basic.model.params.BasicLoadUnloadLaborParam;
import cn.stylefeng.guns.modular.basic.service.BasicLoadUnloadLaborService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;


/**
 * 装卸工劳务公司控制器
 *
 * @author zhangzh
 * @Date 2024-07-18 14:06:45
 */
@Controller
@RequestMapping("/basicLoadUnloadLabor")
public class BasicLoadUnloadLaborController extends BaseController {

    private String PREFIX = "/basic/basicLoadUnloadLabor";

    @Autowired
    private BasicLoadUnloadLaborService basicLoadUnloadLaborService;

    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     *
     * @author zhangzh
     * @Date 2024-07-18
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/basicLoadUnloadLabor.html";
    }

    /**
     * 新增页面
     *
     * @author zhangzh
     * @Date 2024-07-18
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/basicLoadUnloadLabor_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhangzh
     * @Date 2024-07-18
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/basicLoadUnloadLabor_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhangzh
     * @Date 2024-07-18
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BasicLoadUnloadLaborParam basicLoadUnloadLaborParam) {
        BasicLoadUnloadLaborParam q = new BasicLoadUnloadLaborParam();
        q.setCode(basicLoadUnloadLaborParam.getCode());
        int i = basicLoadUnloadLaborService.selectCountByCode(q);
        if(i > 0){
            return ResponseData.error("编码【"+basicLoadUnloadLaborParam.getCode()+"】已存在，不能重复添加");
        }
        q = new BasicLoadUnloadLaborParam();
        q.setNumberPrefix(basicLoadUnloadLaborParam.getNumberPrefix());
        i = basicLoadUnloadLaborService.selectCountByPrefix(q);
        if(i > 0){
            return ResponseData.error("前缀【"+basicLoadUnloadLaborParam.getNumberPrefix()+"】已存在，不能重复添加");
        }
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        basicLoadUnloadLaborParam.setDeliverId(deliverId);
        basicLoadUnloadLaborParam.setDeptId(deptId);
        basicLoadUnloadLaborParam.setCreateUser(user.getUserId());
        basicLoadUnloadLaborParam.setCreateTime(new Date());
        this.basicLoadUnloadLaborService.add(basicLoadUnloadLaborParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhangzh
     * @Date 2024-07-18
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BasicLoadUnloadLaborParam basicLoadUnloadLaborParam) {
        BasicLoadUnloadLaborParam q = new BasicLoadUnloadLaborParam();
        q.setCode(basicLoadUnloadLaborParam.getCode());
        q.setNotId(String.valueOf(basicLoadUnloadLaborParam.getId()));
        int i = basicLoadUnloadLaborService.selectCountByCode(q);
        if(i > 0){
            return ResponseData.error("编码【"+basicLoadUnloadLaborParam.getCode()+"】已存在，不能重复添加");
        }
        q = new BasicLoadUnloadLaborParam();
        q.setNumberPrefix(basicLoadUnloadLaborParam.getNumberPrefix());
        q.setNotId(String.valueOf(basicLoadUnloadLaborParam.getId()));
        i = basicLoadUnloadLaborService.selectCountByPrefix(q);
        if(i > 0){
            return ResponseData.error("前缀【"+basicLoadUnloadLaborParam.getNumberPrefix()+"】已存在，不能重复添加");
        }
        User user = userService.getcurrentUser();
        basicLoadUnloadLaborParam.setUpdateTime(new Date());
        basicLoadUnloadLaborParam.setUpdateUser(user.getUserId());
        this.basicLoadUnloadLaborService.update(basicLoadUnloadLaborParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhangzh
     * @Date 2024-07-18
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BasicLoadUnloadLaborParam basicLoadUnloadLaborParam) {
        this.basicLoadUnloadLaborService.delete(basicLoadUnloadLaborParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhangzh
     * @Date 2024-07-18
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BasicLoadUnloadLaborParam basicLoadUnloadLaborParam) {
        BasicLoadUnloadLabor detail = this.basicLoadUnloadLaborService.getById(basicLoadUnloadLaborParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zhangzh
     * @Date 2024-07-18
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BasicLoadUnloadLaborParam basicLoadUnloadLaborParam,String condition ) {
        return this.basicLoadUnloadLaborService.findPageBySpec(basicLoadUnloadLaborParam,condition);
    }

}


